//
//  DSYLyricTool.m
//  BLDMusics
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "DSYLyricTool.h"

@interface DSYLyricTool ()

@property (nonatomic, assign) NSInteger lyricIndex;

@end

@implementation DSYLyricTool


+ (instancetype)shareLyricTool
{
    static DSYLyricTool *lyricTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lyricTool = [[DSYLyricTool alloc] init];
    });
    return lyricTool;
}

- (void)loadLyricWithString:(NSString *)lyricStr
{
    [self.lyricList removeAllObjects];
//    lyricStr = @"[ti:那时候的我]\n[ar:刘惜君]\n[al:拂晓]\n[offset:500]\n\n[00:00.10]那时候的我-刘惜君\n[00:06.86]作词：李荣浩 \n[00:08.16]作曲：李荣浩\n[00:09.80]专辑拂晓\n[00:11.16]\n[00:14.00]\n[00:18.26]口香糖嚼到有些苦涩\n[00:21.37]抬起头看看天空的颜色\n[00:24.67]头发也许有一点长了\n[00:27.24]戳到我的眼球痒痒的\n[00:30.20]\n[01:57.13][00:31.02]放学后走上回家的路\n[02:00.06][00:34.56]天气热的也不知道几度\n[02:03.50][00:37.60]那边的小孩互相追逐\n[02:06.12][00:40.14]有何独到之处我不清楚\n[02:09.06][00:43.60]\n[02:10.15][00:44.12]满满的日记本里不知道写什么\n[02:16.58][00:50.41]有一些烦恼和忧愁但至少我\n[02:21.92][00:56.16]还剩下快乐 快乐\n[02:24.39][00:58.54]\n[03:04.25][02:24.98][00:58.95]那时候的我\n[03:05.30][02:26.76][01:00.75]总是习惯低着头慢慢往前走\n[03:08.66][02:29.86][01:03.88]看着路边蜗牛\n[03:10.82][02:31.98][01:05.97]那时候的我常常一个人坐在角落\n[03:14.19][02:35.37][01:09.44]回想他刚刚对我说他喜欢我\n[03:17.06][02:38.23][01:12.37]\n[03:17.53][02:38.51][01:12.76]那时候日子过得懒懒的\n[03:20.53][02:41.63][01:15.71]就算整天发呆也不觉得寂寞\n[03:23.74][02:44.88][01:19.06]想到那时候我忍不住有一些难过\n[03:27.06][02:48.19][01:22.20]十七八岁的夏天我现在还能记得\n[03:32.32][02:53.22][01:26.40]\n";
    
//      NSString *lyricStr1 = @"[ti:得意洋洋]\n[ar:方芳]\n[al:龙凤金歌榜VOL.9]\n[offset:0]\n[00:01.24]得意洋洋-方芳\n[00:20.80]清晨的阳光照在我身上\n[00:24.30]\n[00:25.24]温柔地叫我快起床\n[00:28.80]\n[00:30.30]口中还在呢喃 闹钟还没响\n[00:34.30]\n[00:35.55]舍不得离开我的单人床\n[00:38.74]\n[00:45.86]看镜中自己懒懒的模样\n[00:52.80]想今天我会过得怎样\n[00:55.42]会不会像梦里在街上 遇到我心中的偶像\n[00:59.92]\n[01:00.49]衣柜里五颜六色的衣裳\n[01:02.86]该挑出哪件穿在身上\n[01:05.49]外面的天气晴晴朗朗\n[01:07.99]它要我灿烂得和它一样\n[01:10.30]\n[01:10.86]噢啦依耶 我要放声歌唱\n[01:14.74]\n[01:15.67]噢啦依耶耶 我正得意洋洋\n[01:20.05]\n[01:20.67]噢啦依耶 我要放声歌唱\n[01:25.17]\n[01:25.74]噢啦依耶耶 我正得意洋洋\n[01:30.17]\n[01:51.24]衣柜里五颜六色的衣裳\n[01:53.61]该挑出哪件穿在身上\n[01:56.11]外面的天气晴晴朗朗\n[01:58.61]它要我灿烂得和它一样\n[02:01.17]放一段音乐像插上翅膀\n[02:03.43]那天上神仙也不过这样\n[02:05.67]\n[02:06.18]快乐的日子人人向往\n[02:08.68]我喜欢自己现在的小样\n[02:11.30]噢啦依耶 我要放声歌唱\n[02:15.67]\n[02:16.30]噢啦依耶耶 我正得意洋洋\n[02:20.67]\n[02:21.30]噢啦依耶 我要放声歌唱\n[02:25.86]\n[02:26.42]噢啦依耶耶 我正得意洋洋\n[02:30.04]\n[02:41.79]噢啦依耶 如果真的真的这样\n[02:46.35]\n[02:46.91]噢啦依耶耶 于是我得意洋洋\n[02:51.04]\n[02:51.79]噢啦依耶 我要放声歌唱\n[02:56.16]\n[02:56.73]噢啦依耶耶 我正得意洋洋\n[03:01.22]\n[03:01.79]噢啦依耶 我要放声歌唱\n[03:06.79]噢啦依耶耶 我正得意洋洋\n[03:11.29]\n[03:11.85]噢啦依耶 我要放声歌唱\n[03:16.41]噢啦依耶耶 我正得意洋洋\n[03:21.48]\n[03:22.04]噢啦依耶 我要放声歌唱\n[03:26.54]\n[03:27.04]噢啦依耶耶 我正得意洋洋\n[03:31.60]\n[03:32.10]噢啦依耶 我要放声歌唱\n[03:36.54]\n[03:37.17]噢啦依耶耶 我正得意洋洋\n[03:41.60]\n[03:42.17]噢啦依耶\n[03:43.85]歌词制作：  小东 QQ  605192143\n\n";
    
    if (lyricStr == nil || lyricStr.length == 0) {
        return;
    }
    
    NSArray *lyricArr = [lyricStr componentsSeparatedByString:@"\n"];
    
    
    
    for (NSString *aLyricStr in lyricArr) {
        // 根据']'进行拆分
        NSArray *timeAndStr = [aLyricStr componentsSeparatedByString:@"]"];
        
        if ([timeAndStr.firstObject length] == 0) {
            continue;
        }
        
        // 获取时间
        NSString *timeStr = [timeAndStr.firstObject substringFromIndex:1];
        NSArray *timeArray = [timeStr componentsSeparatedByString:@":"];
        NSString *second = timeArray.lastObject;
        NSString *minute = timeArray.firstObject;
        
        NSTimeInterval time = minute.floatValue * 60 + second.floatValue;
        
        // 获取内容
        NSString *string = timeAndStr.lastObject;
        // 组拼为模型
        DSYLyric *lyric = [DSYLyric lyricWithTime:time string:string];
        [self.lyricList addObject:lyric];
        
    }
    
}

- (NSMutableArray *)lyricList
{
    if (!_lyricList) {
        self.lyricList = [NSMutableArray array];
    }
    return _lyricList;
}



- (NSInteger)lyricIndexWithTime:(NSTimeInterval)time
{
    NSTimeInterval currentTime = time * [DSYPlayerTool sharePlayerTool].duration;
    if (currentTime == [DSYPlayerTool sharePlayerTool].duration - 1) {
        [self.lyricList removeAllObjects];
    }
    
    
    int i = 0;
    for (i = 0; i < self.lyricList.count; i++) {
        DSYLyric *lyric = self.lyricList[i];
        
        if (currentTime < lyric.time) {
            self.lyricIndex = (i - 1 > 0) ? (i - 1) : 0;
            break;
        }
    }

        
    return _lyricIndex;
}

@end
